package com.zyy;

/**
 * @ProjectName: concurrent
 * @Package: com.zyy
 * @ClassName: EndThread
 * @Author: ZHAYUYAO
 * @Description: 如何安全的结束Thread
 * @Date: 2019/9/4 22:31
 * @Version: 1.0
 */
public class EndThread {
    static class MyThread extends Thread {
        MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            String currentThreadName = Thread.currentThread().getName();
            while (!isInterrupted()) {
                System.out.println(currentThreadName + " is running");
            }
            System.out.println(currentThreadName + " interrupted flag is " + isInterrupted());

        }
    }

    public static void main(String[] args) {
        Thread myThread = new MyThread("zyy");
        myThread.start();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        myThread.interrupt();
    }
}
